//
//  QYXCardViewController.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/10.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXCardViewController.h"
#import "YSLDraggableCardContainer.h"
#import "CardView.h"
#import "AppDelegate.h"

#define RGB(r, g, b)	 [UIColor colorWithRed: (r) / 255.0 green: (g) / 255.0 blue: (b) / 255.0 alpha : 1]

@interface QYXCardViewController ()<YSLDraggableCardContainerDelegate, YSLDraggableCardContainerDataSource>

@property (nonatomic, strong) YSLDraggableCardContainer *container;
@property (nonatomic, strong) NSMutableArray *datas;


@property (nonatomic, strong) QYXDailyItemListModel *model;
@property (nonatomic, strong) CardView *cardView;
/** 背景图片 */
@property (nonatomic, strong) UIImageView *back;

@end

@implementation QYXCardViewController

{
    BOOL flag;
}




- (void)viewDidLoad
{
    [super viewDidLoad];
    [self requiredData];
    
    UIImage *image = [[UIImage imageNamed:@"back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:@selector(back111)];
    
    self.navigationItem.title = @"精彩探索";
    
    self.back = [[UIImageView alloc] initWithFrame:self.view.bounds];
    [self.view addSubview:_back];
    
    
    
    self.view.backgroundColor = RGB(235, 235, 235);
    
    _container = [[YSLDraggableCardContainer alloc]init];
    _container.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _container.backgroundColor = [UIColor clearColor];
    _container.dataSource = self;
    _container.delegate = self;
    _container.canDraggableDirection = YSLDraggableDirectionLeft | YSLDraggableDirectionRight | YSLDraggableDirectionUp;
    [self.view addSubview:_container];
    
  
    
    
}
- (void)back111
{
    AppDelegate *tempDelegate = [UIApplication sharedApplication].delegate;
    tempDelegate.tabBar.backView.hidden = NO;
    [self.navigationController popViewControllerAnimated:YES];
}



#pragma mark -- YSLDraggableCardContainer DataSource
- (UIView *)cardContainerViewNextViewWithIndex:(NSInteger)index
{
    _cardView = [[CardView alloc]initWithFrame:CGRectMake(25, 90, self.view.frame.size.width - 50, self.view.frame.size.height *0.7)];
    _cardView.backgroundColor = [UIColor whiteColor];
    QYXDailyItemListModel *model = _datas[index];
    [_back sd_setImageWithURL:[NSURL URLWithString:model.cover[@"blurred"]] placeholderImage:kPlaceHolderImage];

    [_cardView.backImageView sd_setImageWithURL:[NSURL URLWithString:model.cover[@"blurred"]] placeholderImage:kPlaceHolderImage];
    [_cardView.imageView sd_setImageWithURL:[NSURL URLWithString:model.cover[@"feed"]] placeholderImage:kPlaceHolderImage];
    _cardView.label.text = model.des;
    _cardView.titleLabel.text =  model.title;
    _cardView.kindLabel.text = [NSString stringWithFormat:@"#%@  /  %ld'%ld''", model.category, model.duration / 60, model.duration % 60];
    ;
    _cardView.numberLabel.text = [NSString stringWithFormat:@"%ld - %ld", index + 1, _datas.count];
    
    if (index == 0) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(self.cardView.center.x - 20, self.cardView.frame.origin.y + self.cardView.frame.size.height + 20, 30, 30)];
        [button setBackgroundImage:[UIImage imageNamed:@"heart"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(likeAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:button];
    }
    
    return _cardView;
}

#pragma mark - 收藏按钮

- (void)likeAction:(UIButton *)button
{
    
    if (flag ) {
        
        [UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:20 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            button.transform = CGAffineTransformMakeScale(1.3, 1.3);
            [button setBackgroundImage:[UIImage imageNamed:@"heart"] forState:UIControlStateNormal];
        } completion:^(BOOL finished) {
             button.transform = CGAffineTransformIdentity;
        }];
        

        
    }
    else
    {
        [UIView animateWithDuration:0.2 delay:0 usingSpringWithDamping:0.3 initialSpringVelocity:20 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
            button.transform = CGAffineTransformMakeScale(1.3, 1.3);
            [button setBackgroundImage:[UIImage imageNamed:@"heartR"] forState:UIControlStateNormal];
        } completion:^(BOOL finished) {
            button.transform = CGAffineTransformIdentity;
        }];

    }
    flag = !flag;
}


- (NSInteger)cardContainerViewNumberOfViewInIndex:(NSInteger)index
{
    return _datas.count;
}

#pragma mark -- YSLDraggableCardContainer Delegate
- (void)cardContainerView:(YSLDraggableCardContainer *)cardContainerView didEndDraggingAtIndex:(NSInteger)index draggableView:(UIView *)draggableView draggableDirection:(YSLDraggableDirection)draggableDirection
{
    if (draggableDirection == YSLDraggableDirectionLeft) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
    if (draggableDirection == YSLDraggableDirectionRight) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
    
    if (draggableDirection == YSLDraggableDirectionUp) {
        [cardContainerView movePositionWithDirection:draggableDirection
                                         isAutomatic:NO];
    }
}

- (void)cardContainderView:(YSLDraggableCardContainer *)cardContainderView updatePositionWithDraggableView:(UIView *)draggableView draggableDirection:(YSLDraggableDirection)draggableDirection widthRatio:(CGFloat)widthRatio heightRatio:(CGFloat)heightRatio
{
    CardView *view = (CardView *)draggableView;
    
    if (draggableDirection == YSLDraggableDirectionDefault) {
        view.selectedView.alpha = 0;
    }
    
    if (draggableDirection == YSLDraggableDirectionLeft) {
        view.selectedView.backgroundColor = RGB(215, 104, 91);
        view.selectedView.alpha = widthRatio > 0.8 ? 0.8 : widthRatio;
    }
    
    if (draggableDirection == YSLDraggableDirectionRight) {
        view.selectedView.backgroundColor = RGB(114, 209, 142);
        view.selectedView.alpha = widthRatio > 0.8 ? 0.8 : widthRatio;
    }
    
    if (draggableDirection == YSLDraggableDirectionUp) {
        view.selectedView.backgroundColor = RGB(66, 172, 225);
        view.selectedView.alpha = heightRatio > 0.8 ? 0.8 : heightRatio;
    }
}




- (void)cardContainerViewDidCompleteAll:(YSLDraggableCardContainer *)container;
{
       UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"哈哈" message:@"今天的探索就到此为止了, 再看一遍?😍" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        AppDelegate *tempDelegate = [UIApplication sharedApplication].delegate;
        tempDelegate.tabBar.backView.hidden = NO;
        [self.navigationController popViewControllerAnimated:YES];

    }]];
    [alert addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [container reloadCardContainer];

    }]];
    
    [self presentViewController:alert animated:YES completion:nil];

}

- (void)cardContainerView:(YSLDraggableCardContainer *)cardContainerView didSelectAtIndex:(NSInteger)index draggableView:(UIView *)draggableView
{
    NSLog(@"++ index : %ld",(long)index);
}


#pragma mark - 网络请求数据

- (void)requiredData
{
    [QYXNetTool getNetWithUrl:@"http://baobab.wandoujia.com/api/v3/recommend?_s=2bdf6768f1a5f2838220505aaa283b17&f=iphone&net=wifi&u=ebe4ca1f9d83e4dc4f915b0495e6e1946ee0eb93&v=2.2.0&vc=721" urlBody:nil header:nil response:QYXJSON success:^(id result) {
        
        self.datas = [NSMutableArray array];
        for (NSDictionary *d in result[@"itemList"]) {
            QYXDailyItemListModel *model = [[QYXDailyItemListModel alloc] init];
            [model setValuesForKeysWithDictionary:d[@"data"]];
            [self.datas addObject:model];
        }
        [_container reloadCardContainer];

    } failure:^(NSError *error) {
        
    }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
